﻿using System;
using System.Collections.Generic;
using System.Linq;
using jQueryApi;

namespace TuringMachine
{
   public delegate void CellEventHandler(StateCell sender, jQueryEvent ev);
   public delegate void CellEventHandlerWithEvent(StateCell sender, jQueryEvent ev);
   public enum HighlightMode { Normal = 0, Hover = 1, Selected = 2 };
   public class StateCell : Div
   {
      public event CellEventHandler OnMouseEnter;
      public event CellEventHandler OnMouseLeave;
      public event CellEventHandlerWithEvent OnMouseMove;
      public event CellEventHandler OnClick;

      private jQueryObject m_movImage;
      private jQueryObject m_altMovImage;
      private jQueryObject m_charImage;
      private jQueryObject m_opImage;

      private int m_character;
      private int m_operation;
      private int m_movement;
      private int m_altMovement;
      private int m_bkgColor;
      private int m_bkgHoverColor;
      private int m_bkgSelectedColor;
      private HighlightMode m_HighlightMode = HighlightMode.Normal;
      private bool m_bHighlighted;
      private bool m_bHovered;

      public int pbkgHoverColor
      {
         get { return m_bkgHoverColor; }
         set
         {
            if (m_bkgHoverColor != value)
            {
               if (m_HighlightMode == HighlightMode.Hover)
                  pObject.RemoveClass(HoverBackgroundClass());
               m_bkgHoverColor = value;
               if (m_HighlightMode == HighlightMode.Hover)
                  pObject.AddClass(HoverBackgroundClass());
            }
         }
      }

      public int pbkgSelectedColor
      {
         get { return m_bkgSelectedColor; }
         set
         {
            if (m_bkgSelectedColor != value)
            {
               if (m_HighlightMode == HighlightMode.Selected)
                  pObject.RemoveClass(SelectedBackgroundClass());
               m_bkgSelectedColor = value;
               if (m_HighlightMode == HighlightMode.Selected)
                  pObject.AddClass(SelectedBackgroundClass());
            }
         }
      }


      public int pBkgNormalColor
      {
         get { return m_bkgColor; }
         set
         {
            if (m_bkgColor != value)
            {
               if (m_HighlightMode == HighlightMode.Normal)
                  pObject.RemoveClass(BackgroundClass());
               m_bkgColor = value;
               if (m_HighlightMode == HighlightMode.Normal)
                  pObject.AddClass(BackgroundClass());
            }
         }
      }


      public int pOperation
      {
         get { return m_operation; }
         set
         {
            m_operation = value;
            m_opImage.RemoveClass();
            if (m_operation >= 0)
            {
               m_opImage.AddClass("cell");
               m_opImage.AddClass("op" + m_operation);
            }
         }
      }

      public bool pbHovered
      {
         get { return m_bHovered; }
         set
         {
            m_bHovered = value;
            UpdateHighlightMode();
         }
      }

      public bool pbHighlighted
      {
         get { return m_bHighlighted; }
         set
         {
            m_bHighlighted = value;
            UpdateHighlightMode();
         }
      }

      private void UpdateHighlightMode()
      {
         if (m_bHovered)
            pHighlightMode = HighlightMode.Hover;
         else
            pHighlightMode = m_bHighlighted ? HighlightMode.Selected : HighlightMode.Normal;
      }

      private HighlightMode pHighlightMode
      {
         get { return m_HighlightMode; }
         set
         {
            if (value != m_HighlightMode)
            {
               pObject.RemoveClass(BackgroundClass());
               pObject.RemoveClass(HoverBackgroundClass());
               pObject.RemoveClass(SelectedBackgroundClass());
               m_HighlightMode = value;
               switch (m_HighlightMode)
               {
                  case HighlightMode.Normal:
                     pObject.AddClass(BackgroundClass());
                     break;
                  case HighlightMode.Hover:
                     pObject.AddClass(HoverBackgroundClass());
                     break;
                  case HighlightMode.Selected:
                     pObject.AddClass(SelectedBackgroundClass());
                     break;
               }
            }
         }
      }

      private string BackgroundClass()
      {
         return "backcellcolor" + m_bkgColor;
      }

      private string HoverBackgroundClass()
      {
         return "backcellcolor" + m_bkgHoverColor;
      }

      private string SelectedBackgroundClass()
      {
         return "backcellcolor" + m_bkgSelectedColor;
      }


      public int pCharacter
      {
         get { return m_character; }
         set
         {
            m_character = value;
            m_charImage.RemoveClass();
            if (m_character >= 0)
            {
               m_charImage.AddClass("cell");
               m_charImage.AddClass("ch" + m_character);
            }
         }
      }

      public int pMovement
      {
         get { return m_movement; }
         set
         {
            m_movement = value;
            m_movImage.RemoveClass();
            if (m_movement >= 0)
            {
               m_movImage.AddClass("cell");
               m_movImage.AddClass("dir" + m_movement);
            }
         }
      }

      public int pAltMovement
      {
         get { return m_altMovement; }
         set
         {
            m_altMovement = value;
            m_altMovImage.RemoveClass();
            if (m_altMovement >= 0)
            {
               m_altMovImage.AddClass("cell");
               m_altMovImage.AddClass("dirt" + m_altMovement);
            }
         }
      }

      public StateCell(jQueryObject parent) :
         base(parent, "cell")
      {
         m_character = -1;
         m_operation = -1;
         m_movement = -1;
         m_altMovement = -1;
         m_bkgHoverColor = 6;
         m_bkgSelectedColor = 7;

         // add background
         pBkgNormalColor = 3;

         // add mov
         string dir_id = "dir_" + pId;
         pObject.Append("<div id=\"" + dir_id + "\"></div>");
         m_movImage = jQuery.Select("#" + dir_id);
         m_movImage.CSS("top", "1px");
         m_movImage.CSS("left", "1px");

         // add altMov
         string dirt_id = "dirt" + pId;
         pObject.Append("<div id=\"" + dirt_id + "\"></div>");
         m_altMovImage = pObject.Children("#" + dirt_id);
         m_altMovImage.CSS("top", "1px");
         m_altMovImage.CSS("left", "1px");

         // add char
         string char_id = "char" + pId;
         pObject.Append("<div id=\"" + char_id + "\"></div>");
         m_charImage = pObject.Children("#" + char_id);
         m_charImage.CSS("top", "1px");
         m_charImage.CSS("left", "1px");

         // add op
         string op_id = "op__" + pId;
         pObject.Append("<div id=\"" + op_id + "\"></div>");
         m_opImage = pObject.Children("#" + op_id);
         m_opImage.CSS("top", "1px");
         m_opImage.CSS("left", "1px");

         pObject.On("mouseenter", delegate(jQueryEvent ev)
         {
            if (OnMouseEnter != null)
               OnMouseEnter(this, ev);
         });
         pObject.On("mouseleave", delegate(jQueryEvent ev)
         {
            if (OnMouseLeave != null)
               OnMouseLeave(this, ev);
         });


         pObject.MouseMove(delegate(jQueryEvent ev)
         {
            if (OnMouseMove != null)
               OnMouseMove(this, ev);
         });

         pObject.Click(delegate(jQueryEvent ev)
         {
            if (OnClick != null)
               OnClick(this, ev);
         });

      }



      public void SetThickBorder(bool bActive)
      {
         if (bActive)
            pObject.AddClass("curractive");
         else
            pObject.RemoveClass("curractive");
      }

      public void GetDataFrom(StateCell other)
      {
         pOperation = other.pOperation;
         pCharacter = other.pCharacter;
         pMovement = other.pMovement;
         pAltMovement = other.pAltMovement;
         pBkgNormalColor = other.pBkgNormalColor;
      }

   }

}
